package com.platform.common.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 生成唯一ID
 * @author 郭少
 */
public class UniqueIdGenerate {

    // TODO 增长ID 缓存key
    private static final String AUTO_ID = "AUTO_ID";
    // TODO 订单缓存key
    private static final String ORDER_ID = "ORDER_ID";
    // TODO 订单ID前缀
    private static final String ORDER_ID_PREFIX = "OD";
    // TODO 时间格式化
    private static final String DATE_FORMAT = "yyyyMMddHHmmss";


    private static RedisTemplate<String, Object> redisTemplate = SpringContextUtils.getBean("redisTemplate", RedisTemplate.class);

    /**
     * 通过key获取自增
     *
     * @param key
     * @return
     */
    public static Long generateId(String key) {
        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        return counter.incrementAndGet();
    }

    /**
     * 通过key获取自增并设定过期时间
     *
     * @param key
     * @param date 过期时间
     * @return
     */
    public static Long generateId(String key, Date date) {
        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        counter.expireAt(date);
        return counter.incrementAndGet();
    }

    /**
     * 获取唯一ID
     *
     * @return
     */
    public static Long getUniqueID() {
        StringBuffer sb = new StringBuffer();
        int maxLen = 8;
        //生成16位ID
        //1000000002326747
        Long generateId = generateId(AUTO_ID);
        int len = String.valueOf(generateId).length();
        if (len < maxLen) {
            sb.append("1");
            // TODO 长度小于16进行补0
            for (int i = 0; i < maxLen - (len + 1); i++) {
                sb.append("0");
            }
        }
        sb.append(generateId);
        return Long.valueOf(sb.toString());
    }

    /**
     * 获取唯一订单号
     *
     * @return
     */
    public static String genOrderId() {
        Long id = generateId(ORDER_ID, getExpireAtData());
        String orderId = format(id, ORDER_ID_PREFIX);
        return orderId;
    }

    /**
     * 过期时间
     *
     * @return
     */
    public static Date getExpireAtData() {
        Date date = null;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        date = calendar.getTime();
        return date;
    }

    /**
     * 格式化时间
     *
     * @param format 时间格式
     * @return
     */
    public static String getDateFormat(String format) {
        DateFormat df = new SimpleDateFormat(null == format || "".equals(format) ? DATE_FORMAT : format);
        return df.format(new Date());
    }


    /**
     * 自定义ID格式
     *
     * @param id
     * @param prefix 前缀
     * @return
     */
    private static String format(Long id, String prefix) {
        int minLength = 20;
        StringBuffer sb = new StringBuffer();
        if (null != prefix && !"".equals(prefix)) {
            sb.append(prefix);
        }
        sb.append(getDateFormat(null));

        String strId = sb.toString() + "" + id;
        int length = strId.length();

        if (length < minLength) {
            for (int i = 0; i < minLength - length; i++) {
                sb.append("0");
            }
        }
        sb.append(id);
        return sb.toString();
    }


}
